import { get, post } from '@/utils/http'
import type { BasicPagingListParams, BasicPagingListResult } from '@/apis/basicModel'
import type {
  GetAccountListResult,
  GetAllowanceByYearResult,
  CreateCarbonTransactionParams,
  GetAccountChangeRecordParams,
  GetAccountChangeRecordResult,
  CreateAssetsSubAccountParams
} from './model/accountModel'
enum Api {
  /**
   * 碳资产账户列表
   */
  GET_ASSET_LIST = '/book/carbonAsset/listData',
  /**
   * 获取资产单位年剩余额度
   */
  GET_ALLOWANCE_BY_YEAR = '/book/carbonAsset/findAllowanceByYear',
  /**
   * 根据资产账户和年份查询配额余量
   */
  GET_ACCESS_QUOTA_BY_NO_AND_YEAR = '/book/carbonAsset/getAccessQuotaByNoAndYear',
  /**
   * 新增账户交易记录
   */
  CREATE_TRANSACTION = '/book/carbonTransaction/createCarbonTransaction',
  /**
   * 排放单位列表
   */
  GET_DISCHARGE_UNIT_LIST = '/book/carbonInspect/dischargeUnitList',
  /**
   * 账户余额可用量
   */
  GET_ASSET_BALANCE = '/book/carbonTransaction/carbonAssetInfo',
  /**
   * 碳资产账户流水分页
   */
  GET_ACCOUNT_CHANGE_RECORD = '/book/carbonTransaction/pageTransaction',
  /**
   * 拉取能碳数据
   */
  PULL_POWER_DATA = '/book/energySummary/pullData',
  /**
   * 添加子账户
   */
  CREATE_ASSETS_SUB_ACCOUNT = '/book/carbonAsset/createAssetSubAccount'
}

// 碳资产列表
export const getAccountTree = (): Promise<GetAccountListResult[]> => get(Api.GET_ASSET_LIST)

/**
 * 获取资产单位年剩余额度
 * @param assetNo	资产账户
 * @returns
 */
export const getAllowanceByYear = (assetNo: string): Promise<GetAllowanceByYearResult[]> =>
  get(Api.GET_ALLOWANCE_BY_YEAR, { assetNo })

/**
 * 根据资产账户和年份查询配额余量
 * @param assetNo 资产账户
 * @param assetType 资产类型
 * @param year 年份
 * @returns
 */
export const getAccessQuotaByNoAndYear = (
  assetNo: string,
  assetType: string,
  year?: string
): Promise<number> => get(Api.GET_ACCESS_QUOTA_BY_NO_AND_YEAR, { assetNo, assetType, year })

/**
 * 新建碳资产交易记录
 */
export const createCarbonTransaction = (params: CreateCarbonTransactionParams): Promise<void> =>
  post(Api.CREATE_TRANSACTION, params)

/**
 * 排放单位列表
 */
export const getDischargeUnitList = (): Promise<
  { organizationCode: string; organizationName: string }[]
> => get(Api.GET_DISCHARGE_UNIT_LIST)

/**
 * 账户余额可用量
 */
export const getAssetBalance = (params: any): Promise<number> => get(Api.GET_ASSET_BALANCE, params)

/**
 * 碳账户变动记录
 * @param assetNo 编号
 * @returns
 */
export const getAccountChangeRecord = (
  params: GetAccountChangeRecordParams & BasicPagingListParams
): Promise<BasicPagingListResult<GetAccountChangeRecordResult>> =>
  get(Api.GET_ACCOUNT_CHANGE_RECORD, params)

/**
 * 拉取能碳数据
 */
export const pullData = (params: any): Promise<any> => get(Api.PULL_POWER_DATA, params)

/**
 * 添加子账户
 */
export const createAssetsSubAccount = (params: CreateAssetsSubAccountParams): Promise<void> =>
  post(Api.CREATE_ASSETS_SUB_ACCOUNT, params)
